package com.shengruan.servlet;

import java.io.IOException;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.RandomStringUtils;

import com.shengruan.framework.common.lib.captcha.servlet.CaptchaProducer;
import com.shengruan.framework.common.lib.captcha.servlet.DefaultCaptchaIml;

public class ValidateCodeServlet extends HttpServlet{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;


	public void doGet(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		doPost(request, response);

	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		try {   
			String s = RandomStringUtils.random(4, true, true);   
			HttpSession session = request.getSession(true);   
			session.setAttribute("validateCode", s);
			response.setContentType("images/jpeg");   
			response.setHeader("Pragma", "No-cache");   
			response.setHeader("Cache-Control", "no-cache");   
			response.setDateHeader("Expires", 0);   
			ServletOutputStream out = response.getOutputStream();   
			Properties p = new Properties();
			CaptchaProducer c = new DefaultCaptchaIml(p);
			p.put("cap.obscurificator", "com.shengruan.common.lib.captcha.obscurity.imp.WaterRiple");
			c.createImage(out, s);
			out.close();   
		} 
		catch (Exception e) 
		{   
			//e.printStackTrace();   
		}  

	}


}
